package net.sudot.commons.security;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.realm.Realm;

/**
 * 返回第一个认证成功的认证信息，并且遇到异常不忽略，直接抛出
 *
 * @author tangjialin on 2019-01-21.
 */
public class FirstSuccessfulStrategy extends org.apache.shiro.authc.pam.FirstSuccessfulStrategy {

    @Override
    public AuthenticationInfo afterAttempt(Realm realm, AuthenticationToken token, AuthenticationInfo singleRealmInfo, AuthenticationInfo aggregateInfo, Throwable t) throws AuthenticationException {
        if (t != null) {
            throw t instanceof AuthenticationException ? (AuthenticationException) t : new AuthenticationException(t);
        }

        return super.afterAttempt(realm, token, singleRealmInfo, aggregateInfo, t);
    }
}
